<?php
class info_pyq extends base{

	public function init(){
		parent::init();

		$this->cityObj = D('city');
		$this->cateObj = D('cate');
		$this->userObj = D('user');
		$this->upload_fileObj = D('upload_file');
		$this->adObj = D('ad');

		$this->modelObj = D('info_pyq');
		$this->logObj = D('info_pyq_log');
		$this->tagObj = D('info_pyq_tag');
	}

	public function siteinfo() {
		if($this->user){
			//站长
			// $data['siteadmin'] = $this->get_site_admin($this->city['id'],$this->cate['id']);
		}

		// $result = $this->adObj->get_ad($this->city_id,'house_zf');
		// if($result){
		// 	$site['adimg'] = $result;
		// 	$ads['adimg'] = $result;
		// }
		$site = array(
			'name' => $this->city['shortname'].'圈圈房',
			'title' => '精选租房信息',
			'logo' => $this->upload_fileObj->get_image($this->setting['site']['site_logo'],1),
			'adimg' => $this->adObj->get_ad($this->city_id,'info_pyq'),
		);

		$data['site'] = $site;
		// $data['ads'] = $ads;
		$data['city'] = $this->city;
		$this->renderSuccess('获取成功','',$data);
	}

	//列表
	public function index() 
	{
		
		$data = array();
		$city_id = arg('city_id');
		$area_id = arg('area_id');
		$cate_id = arg('cate_id');
		$tag_id = arg('tag_id');
		$keyword = arg('keyword');
		$t_id = arg('t_id');
		
		$cond = "status=1 ".$this->cond_city;

		if($area_id){
			$cond .=" and area_id={$area_id}";
		}
		if($cate_id){
			$cond .=" and cate_id={$cate_id}";
		}
		if($tag_id){
			$cond .=" and FIND_IN_SET($tag_id,tags)";
		}
		if($keyword){
			$cond.=" and (title like '%".$keyword."%' or intro like '%".$keyword."%') ";
		}
		$result = $this->modelObj->get_datalist($cond,'is_top desc,up_time desc,id desc');
		if($result){
			$data['list'] = $result['list'];
			//访问房源
			// if($info_id){
			// 	if($this->user['id']){
			// 		//每次积分
			// 		$score = $this->site_setting['house']['score_num'];
			// 		$score = 10;
			// 		$data['log_id'] = $this->house_logObj->dianzan($info_id,$this->user['id'],'访问了房源',$t_id,$score);
			// 		//增加积分
			// 		if($t_id){
			// 			// $this->userObj->add_score($t_id,'score',10);
			// 		}
			// 	}
			// }
		}
		
		$this->renderSuccess('获取成功','',$data);
		
	}


	public function add(){

		if($this->fmdo == 'add'){

			$user_id = $this->user['id'];
			$formData = $this->formData('formData');

			$checkText = $this->userObj->checkText($this->setting['wxapp'],arg('formData'));
			if($checkText['errcode'] == 87014){
				$this->renderError('提交内容不安全');
			}

			$intro = $formData['intro'];


			$tag = $formData['tag'];
			if($tag){
				$tags = $this->tagObj->add_tag($this->city_id,$tag);

				// foreach($tag as $val){
				// 	$tags[] = $this->tags[$val]['id'];
				// }
				// $tags = joinStr($tags);
			}

			$arrBase = array(
				'city_id' => 1,
				'area_id' => 1,
				'cate_id' => 1,
				// 'city_id' => $formData['city_id'],
				// 'area_id' => $formData['area_id'],
				// 'cate_id' => $formData['cate_id'],
				'title' => $formData['title'],
				'intro' => $formData['intro'],
				'photo' => joinStr($formData['urls']),
				'tags'=>$tags,
				'add_time' => date('Y-m-d H:i:s'),
				'user_id' => $user_id,
			);

			// dump($arrBase);exit;
			$info_id = $this->modelObj->add($arrBase);
			if($info_id){
				$data['info_id'] = $info_id;
				$this->renderSuccess('发布成功','',$data);
			}
			
		}
		
	}


	public function dianzan(){
		if($this->fmdo){
			$order_status = 1;

			$info_id = arg('info_id');
			$user_id = $this->user['id'];

			if($info_id && $user_id){
				$log_id = $this->logObj->dianzan($info_id,$user_id);

				$data['log_id'] = $log_id;
				$this->renderSuccess('点赞成功','',$data);
			}
		}
	}

	public function pinglun(){
		if($this->fmdo){
			$order_status = 1;

			
			$formData = $this->formData('formData');
			$user_id = $this->user['id'];
			$to_user_id = $formData['to_user_id'] ?? 0 ;
			$info_id = $formData['info_id'];
			// dump($formData);exit;

			if($info_id && $user_id){
				$log_id = $this->logObj->pinglun($info_id,$user_id,$to_user_id,$formData['msg']);

				$data['log_id'] = $log_id;
				$this->renderSuccess('点赞成功','',$data);
			}
		}
	}
}